<%@ page language="java" import="com.newcrm.config.Const,com.newcrm.util.Util,com.newcrm.bean.*,java.util.List,java.util.Map" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
  User user = (User) session.getAttribute("user");
  List<User> users = (List<User>)request.getAttribute("users");
  Map<Integer, CompanyInfo> companys = (Map<Integer, CompanyInfo>) application.getAttribute(Const.APPLICATION_COMPANYS);
  CompanyInfo companyInfo = companys.get(Integer.valueOf(user.companyId));
  List<Group> allGroups = companyInfo.getAllGroups();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>员工管理</title>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <meta name="renderer" content="webkit"/>
  <meta http-equiv="X-UA-Compatible" content="IE=Edge">
  <link rel="shortcut icon" href="<%=request.getContextPath()%>/images/qikebao.ico" />
  <link rel="stylesheet" href="<%=request.getContextPath()%>/css/all.css?ver=<%=Const.VERSION%>">
  <link rel="stylesheet" href="<%=request.getContextPath()%>/css/font-awesome/css/font-awesome.min.css">
  <link rel="stylesheet" href="<%=request.getContextPath()%>/css/datepicker.css" />
  <link rel="stylesheet" href="<%=request.getContextPath()%>/css/bootstrap-datetimepicker.min.css" />
  <link rel="stylesheet" href="<%=request.getContextPath()%>/css/magnific-popup.css">
  <script type="text/javascript" src="<%=request.getContextPath()%>/script/jquery-1.11.1.min.js"></script>
  <script type="text/javascript" src="<%=request.getContextPath()%>/script/bootstrap.min.js"></script>
  <script type="text/javascript" src="<%=request.getContextPath()%>/script/jquery-ui.min.js"></script>
  <script type="text/javascript" src="<%=request.getContextPath()%>/script/jquery.bootstrap-growl.min.js"></script>
  <script type="text/javascript" src="<%=request.getContextPath()%>/script/moment.min.js"></script>
  <script type="text/javascript" src="<%=request.getContextPath()%>/script/bootstrap-datepicker.js"></script>
  <script type="text/javascript" src="<%=request.getContextPath()%>/script/bootstrap-datetimepicker.min.js"></script>
  <script type="text/javascript" src="<%=request.getContextPath()%>/script/daterangepicker.js"></script>
  <script type="text/javascript" src="<%=request.getContextPath()%>/script/jquery.magnific-popup.min.js"></script>
  <script type="text/javascript" src="<%=request.getContextPath()%>/script/jquery.cookie.js"></script>
  <script type="text/javascript" src="<%=request.getContextPath()%>/script/popbox.js"></script>
  <script type="text/javascript" src="<%=request.getContextPath()%>/script/common.js?ver=<%=Const.VERSION%>"></script>
  <script type="text/javascript" src="<%=request.getContextPath()%>/script/userManager.js?ver=<%=Const.VERSION%>"></script>
  <script>
    $(function() {
      $('#identity_group').val("<%=request.getAttribute("groupId")%>");
      $("#groupView").text($("#identity_group option:selected").text());
    });
  </script>
</head>
<body class="parties">
  <jsp:include page="topNavi.jsp" flush="true"/>
  <div class="main-page">
    <table>
    <tr>
      <td class="main-body">
        <div class="topnavi">
          <div class="popbox rightMenu">
            <a class='open' href="#"><i class="fa fa-users"></i> <span id="groupView">所有部门</span> <i class="fa fa-chevron-down"></i></a>
            <div class='collapse'>
              <div class='box'>
                <div class='arrow'></div><div class='arrow-border'></div>
                <form>
                <h1>请选择部门：</h1>
                <select id="identity_group"><option value="0">所有部门</option>
                <%if (user.isAdmin) { %>
                <%
                    if (allGroups != null && allGroups.size() > 0) {
                      for (Group group:allGroups) {%>
                <option value="<%=group.getId()%>"><%=Util.HTMLEncode(group.getName())%></option>
                <%    }
                    }
                } else {
                    Group group = companyInfo.getGroupById(user.groupId); %>
                    <option value="<%=group.getId()%>"><%=Util.HTMLEncode(group.getName())%></option>
                <%
                    List<Group> childrenGroups = group.getAllChildren();
                    if (childrenGroups != null) {
                      for (Group childGroup:childrenGroups) {%>
                <option value="<%=childGroup.getId()%>"><%=Util.HTMLEncode(childGroup.getName())%></option>
                <%}}}%>
                </select></form>
              </div>
            </div>
          </div>
          <div class='leftMenu'><span>员工管理</span></div>
        </div>
        <div class="toolbar">
          <a id="selectall-link" title="全选/全不选" href="#" class="button grey small"><i class="fa fa-check-square-o"></i> 全选</a>
          <a href="<%=request.getContextPath()%>/users/new?groupId=<%=user.groupId%>" title="创建新员工" class="button grey small"><i class="fa fa-plus"></i> 创建新员工</a>
          <a message="请选择需要设置的客户" href="#changeMaxCompanyNum-popup" title="修改员工名下客户上限" class="popup-link button grey small"><i class="fa fa-sort-numeric-asc"></i> 修改客户上限</a>
        </div>
        <div id="recordings" class="recordings">
          <%if (users == null || users.size() == 0) {%>
          <p>该部门下没有员工信息。</p>
          <%} else {%>
          <table id="users" class="users">
          <%
            for (User u:users) {
              String isAdmin = u.isAdmin ? "checked='checked'" : "";
              String canExport = u.canExport ? "checked='checked'" : "";
              String canDelete = u.canDelete ? "checked='checked'" : "";
              String isGroupAdmin = u.isGroupAdmin ? "1" : "0";
          %>
          <tr>
            <td class="person">
              <div class="avatar_subject">
                <div class="chk">
                  <input type="checkbox" value="<%=Util.HTMLEncode(u.email)%>" userId="<%=u.userId%>" userName="<%=Util.HTMLEncode(u.name)%>">
                </div>
                <div class="avatar">
                  <img src="<%=request.getContextPath()%>/<%=u.imgPath%>" class="avatar" width="55" height="55">
                </div>
                <div class="info">
                  <h3><a href="<%=request.getContextPath()%>/users/<%=u.userId%>/edit"><%=Util.HTMLEncode(u.name)%> <span>（<%=u.userId%>）</span></a></h3>
                  <h6><a href="mailto:<%=Util.HTMLEncode(u.email)%>" class="mailaddress"><%=Util.HTMLEncode(u.email)%></a></h6>
                  <h6 id="user_group_<%=u.userId%>"><%String groupName = companyInfo.getGroupName(u);
                    if (groupName != null && groupName.length() > 0) { %>所属部门：<%=Util.HTMLEncode(groupName)%><%if (u.isGroupAdmin) {%> (<span style="color:red;">部门主管</span>)<%}%>
                    <%}%></h6>
                </div>
              </div>
            </td>
            <td>
              <%String maxCompanyNum = u.extInfo.get(Const.USER_EXT_MAXCOMPANYNUM);
                if (maxCompanyNum != null && maxCompanyNum.length() > 0 && (!"0".equals(maxCompanyNum))) { %>
                <h6>客户上限:<b><%=maxCompanyNum%></b></h6>
              <% }%>
            </td>
            <td class="admin">
              <%if(!u.isCreater) {%>
              <div class="toggles" id="<%=u.userId%>">
                <% if (user.isSuperAdmin()) {%>
                <p>
                  <label>
                    <input name="user_admin" type="checkbox" <%=isAdmin%> value="1" />
                    <span>管理员</span>
                  </label>
                  <label>
                    <input name="user_export" type="checkbox" <%=canExport%> value="1" />
                    <span>可以导出</span>
                  </label>
                </p>
                <p>
                  <label>
                    <input name="user_delete" type="checkbox" <%=canDelete%> value="1" />
                    <span>可以删除</span>
                  </label>
                </p>
                <%}%>
                <p style="margin-top:6px;">
                  <a title="编辑" href="<%=request.getContextPath()%>/users/<%=u.userId%>/edit">编辑</a> |
                  <a title="修改员工登录密码" href="<%=u.userId%>" n="<%=Util.HTMLEncode(u.name)%>" class="changepwd-popup-modal">修改密码</a> |
                  <%if (user.isAdmin) { %><a title="修改部门" href="<%=u.userId%>" groupId="<%=u.groupId%>" groupAdmin="<%=isGroupAdmin%>" n="<%=Util.HTMLEncode(u.name)%>" class="changegroup-popup-modal">修改部门</a> | <%}%>
                  <a title="将该员工名下客户转移至其他员工名下" href="<%=u.userId%>" n="<%=Util.HTMLEncode(u.name)%>" class="changeowner-popup-modal">转移客户</a>
                </p>
              </div>
              <%} else {%>
              <div class="toggles">创建者</div>
              <%}%>
            </td>
            <td class="edit">
            <%if(!u.isCreater) {%>
              <a class="delete-user-link" title="删除员工" href="<%=u.userId%>"><i class="fa fa-trash-o"></i></a>
            <%}%>
            </td>
          </tr>
          <%}%>
          </table>
          <%}%>
        </div>
      </td>
      <td class="main-sidebar">
        <div class="content">
          <div class="liner">
            <div class="sidebox closedbox">
              <h5>管理员具有什么权限？</h5>
              <p>管理员可以创建，修改和删除员工，并且可以查看，修改和删除所有的客户信息，备注信息等。</p>
            </div>
          </div>
        </div>
      </td>
    </tr>
    </table>
  </div>

  <div id="changepwd-popup" class="white-popup mfp-hide">
    <div class="popup-title"><span class="title">修改员工 [<span class="userName" id="changepwd-userName"></span>] 的登录密码</span> <span class="popup-close-button"><i class="fa fa-times"></i>&nbsp;</span></div>
    <div class="popup-content">
      <table>
        <tr class="username_password">
          <th><label for="identity_password">密码</label></th>
          <td>
            <div class="validated_field">
              <p class="field"><input id="identity_password" name="identity_password" size="30" tabindex="1" type="password" /></p>
            </div>
          </td>
        </tr>
        <tr class="username_password confirm_password">
          <th nowrap><label for="identity_password_confirmation">确认密码</label></th>
          <td>
            <div class="validated_field">
              <p class="field"><input id="identity_password_confirmation" name="identity_password_confirmation" size="30" tabindex="2" type="password" /></p>
            </div>
          </td>
        </tr>
      </table>
    </div>
    <div class="buttonbar">
      <div class="right">
        <a id="changepwd-link" class="button blue small" href="#"><i class="fa fa-check"></i> 确定</a>
        <a class="button grey small popup-close-button" href="#"><i class="fa fa-times"></i> 取消</a>
      </div>
      <div class="message"></div>
    </div>
  </div>

  <%if (user.isAdmin) { %>
  <div id="changegroup-popup" class="white-popup mfp-hide">
    <div class="popup-title"><span class="title">修改员工 [<span class="userName" id="changegroup-userName"></span>] 的部门</span> <span class="popup-close-button"><i class="fa fa-times"></i>&nbsp;</span></div>
    <div class="popup-content">
      <div>部门
      <select id="user_group" name="user_group">
        <option value="0">无</option>
        <%  if (allGroups != null && allGroups.size() > 0) {
              for (Group group:allGroups) {%>
        <option value="<%=group.getId()%>"><%=Util.HTMLEncode(group.getName())%></option>
        <%    }
            }%>
      </select>
      <span id="isGroupAdminSpan" style="display:none;"><input type="checkbox" name="user_isGroupAdmin" id="user_isGroupAdmin" value="1"><label for="user_isGroupAdmin">部门主管</label></span>
    </div>
    <div class="buttonbar">
      <div class="right">
        <a id="changegroup-link" class="button blue small" href="#"><i class="fa fa-check"></i> 确定</a>
        <a class="button grey small popup-close-button" href="#"><i class="fa fa-times"></i> 取消</a>
      </div>
      <div class="message"></div>
    </div>
  </div>
  <%}%>

  <div id="changeowner-popup" class="white-popup mfp-hide">
    <div class="popup-title"><span class="title">转移员工 [<span class="userName" id="changeowner-userName"></span>] 名下的客户</span> <span class="popup-close-button"><i class="fa fa-times"></i>&nbsp;</span></div>
    <div class="popup-content">
      <p>该员工名下的所有公司/联系人/销售机会等信息将会被转移到选择后的目标员工名下。</p>
      <div>转移到员工 <select id="modifyUserOwnerSel">
      <%if (users != null || users.size() > 0) {
          for (User u:users) {%>
        <option value="<%=u.userId%>"><%=Util.HTMLEncode(u.name)%></option>
      <%  }
        }%>
      </select></div>
    </div>
    <div class="buttonbar">
      <div class="right">
        <a id="changeowner-link" class="button blue small" href="#"><i class="fa fa-check"></i> 确定</a>
        <a class="button grey small popup-close-button" href="#"><i class="fa fa-times"></i> 取消</a>
      </div>
      <div class="message"></div>
    </div>
  </div>

  <div id="changeMaxCompanyNum-popup" class="white-popup mfp-hide">
    <div class="popup-title"><span class="title">设置员工名下的客户上限</span> <span class="popup-close-button"><i class="fa fa-times"></i>&nbsp;</span></div>
    <div class="popup-content">
      <div>客户上限: <input type="text" id="maxCompanyNum"></div>
      <p><br/>超过该上限后，该员工无法新创建客户</p>
    </div>
    <div class="buttonbar">
      <div class="right">
        <a id="changeMaxCompanyNum-link" class="button blue small" href="#"><i class="fa fa-check"></i> 确定</a>
        <a class="button grey small popup-close-button" href="#"><i class="fa fa-times"></i> 取消</a>
      </div>
      <div class="message"></div>
    </div>
  </div>
</body>
</html>